package com.skyline.common.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.skyline.common.util.Constant;
import com.skyline.common.util.WebHelper;

/**
 * 生成验证码图片，在网上一版本之上进行修改
 * 
 * @author Jairus Chan
 * @version 0.1
 */
public class MakeCertPic extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2345485492283930946L;

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
	 * methods.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	private static char mapTable[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
			'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		response.setContentType("image/jpeg");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		// ���ڴ��д���ͼ��
		int width = 120, height = 40;
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

		// ��ȡͼ��������
		Graphics graphics = image.getGraphics();

		// ��������
		Random random = new Random();

		// �趨����ɫ
		graphics.setColor(getRandColor(200, 250));
		graphics.fillRect(0, 0, width, height);

		// �趨����
		graphics.setFont(new Font("Times New Roman", Font.BOLD, 32));

		// ���߿�
		// g.setColor(new Color());
		// g.drawRect(0,0,width-1,height-1);

		// ������100�������ߣ�ʹͼ���е���֤�벻�ױ��������̽�⵽
		graphics.setColor(getRandColor(160, 200));
		for (int i = 0; i < 100; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			int xl = random.nextInt(40);
			int yl = random.nextInt(40);
			graphics.drawLine(x, y, x + xl, y + yl);
		}

		// ȡ���������֤��(4λ����)
		StringBuilder sRand = new StringBuilder();
		for (int i = 0; i < 4; i++) {
			int rand = random.nextInt(36);
			sRand.append(mapTable[rand]);
			// ����֤����ʾ��ͼ����
			graphics.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));// ���ú����������ɫ��ͬ����������Ϊ����̫�ӽ�����ֻ��ֱ�����
			graphics.drawString(mapTable[rand] + "", 30 * i + 10, getRandInt(20, 40));
		}

		// ����֤�����SESSION
		WebHelper.setSessionAttribute(request, Constant.SESSION_CERT_CODE, sRand.toString());
		// ͼ����Ч
		graphics.dispose();
		ServletOutputStream responseOutputStream = response.getOutputStream();
		// ���ͼ��ҳ��
		ImageIO.write(image, "JPEG", responseOutputStream);

		// ���¹ر���������
		responseOutputStream.flush();
		responseOutputStream.close();
	}

	int getRandInt(int b, int t) {
		Random random = new Random();
		return b + random.nextInt(t - b);
	}

	Color getRandColor(int fc, int bc) {// ��Χ��������ɫ
		Random random = new Random();
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int red = fc + random.nextInt(bc - fc);
		int green = fc + random.nextInt(bc - fc);
		int blue = fc + random.nextInt(bc - fc);
		return new Color(red, green, blue);
	}

	// <editor-fold defaultstate="collapsed"
	// desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		processRequest(request, response);
	}

	/** Returns a short description of the servlet. */
	public String getServletInfo() {
		return "Short description";
	}
	// </editor-fold>
}
